# -*- encoding:utf-8 -*-

import time
import threading

class myThread(threading.Thread):
    def __init__(self,period = 0.1):
        threading.Thread.__init__(self,daemon=True)
        self.__running = threading.Event()
        self.__running.set()
        self.__period = period
        self.__runable = threading.Event()
        self.__runable.set()

    def initproc(self):
        pass
    def loop(self):
        pass
    def endproc(self):
        pass
    def run(self):
        self.initproc()
        while self.__runable.is_set() & self.__running.wait():
            self.loop()
            time.sleep(self.__period)
        self.endproc()

    def pause(self):
        self.__running.clear()
    def stop(self):
        self.__runable.clear()
        self.join(timeout = 0.1)

class KvRcvMsgThread(myThread):
    count = 1
    def initproc(self):
        print("start")
    def loop(self):
        print(self.count)
        self.count += 1
    def endproc(self):
        print("end")



if __name__ == "__main__":
    x = KvRcvMsgThread(period=0.1)
    x.start()
    time.sleep(1)
    x.stop()